(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.jobGroup.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'modalService',
			'jobGroupService' ];
	function deleteController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, modalService, jobGroupService) {
		var ctrl = this, jobGroupInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.jobGroup = {};
		ctrl.jobGroup.jobgrpDepencyList = ctrl.jobGroup.jobgrpDepencyList || [];
		ctrl.jobGroup.jobgrpSchd = ctrl.jobGroup.jobgrpSchd || {};
		ctrl.jobGroup.jobGroupPreParmetes = ctrl.jobGroup.jobGroupPreParmetes || [];
		ctrl.jobGroup.jobGroupPostParmetes = ctrl.jobGroup.jobGroupPostParmetes || [];
		ctrl.init = function() {
			var params = {
				jobGroupId : jobGroupInfo.jobGroupId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobgrpDepency.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.jobGroup.jobgrpDepencyList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			
			ctrl.jobPreParmetesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobGroupPreParameters.tableConfigs.table);
			ctrl.jobPreParmetesOptions.data = 'vm.jobGroup.jobGroupPreParmetes';
			ctrl.jobPreParmetesOptions.enableRowHeaderSelection = false;
			
			ctrl.jobPostParmetesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobGroupPostParameters.tableConfigs.table);
			ctrl.jobPostParmetesOptions.data = 'vm.jobGroup.jobGroupPostParmetes';
			ctrl.jobPostParmetesOptions.enableRowHeaderSelection = false;
			
			jobGroupService.initDataOptions(ctrl);
			
			authService.delete(params).then(function(response) { 
				ctrl.jobGroup = response.data || jobGroupInfo; 
			});
			 
		};

		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.jobGroup.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.jobGroup).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};

		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.jobGroup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.jobgroup.list',
					'state' : 'cda.jobGroup.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);